<?php

declare(ENCODING = 'UTF-8');

/*                                                                        *
 * This script is part of the TYPO3 project - inspiring people to share!  *
 *                                                                        *
 * TYPO3 is free software; you can redistribute it and/or modify it under *
 * the terms of the GNU General Public License version 2 as published by  *
 * the Free Software Foundation.                                          *
 *                                                                        *
 * This script is distributed in the hope that it will be useful, but     *
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN-    *	
 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General      *
 * Public License for more details.                                       *
 *                                                                        */	

/**
 * @package auxo
 * @subpackage AOP
 * @version $Id$
 */

/**	
 * The AOP Pointcut Expression Tokenizer
 *	
 * @package auxo
 * @subpackage AOP
 * @version $Id$	
 * @copyright Copyright belongs to the respective authors
 * @author AHN
 * @license http://opensource.org/licenses/gpl-license.php GNU Public License, version 2
 */
class tx_auxo_AOPPointcutExpressionTokenizer extends tx_auxo_AOPPointcutExpression {
	/**
	 * An array of generated tokens
	 *
	 * @var array
	 */
	private $tokens = array();
	
	/**
	 * Expression that splits a string into tokens
	 *
	 * @var string
	 */
	private $splitExpression = '/(\(|\)|\|\||\&\&| |,|\.\.|\->|!)/';
	
	/**
	 * Creates a new tokenizer
	 *
	 * @param string $input
	 */
	public function __construct($input) {
		$result = preg_split($this->splitExpression, $input, 0, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
		/*
		 * remove empty items from result array because for some unknown reasons it is not 
		 * enough to set PREG_SPLIT_NO_EMPTY
		 */
		foreach($result as $item) {
			if (strlen(trim($item))) $this->tokens[] = $item;
		}
	}
	
	/**
	 * Returns either the current token or a given token
	 *
	 * @param int $offset number of token to look ahead 0 = current, 1 = next, etc.
	 * @return int $token a token id
	 */
	public function get($offset=0) {
		if ($offset > count($this->tokens)) {
			return self::TOKEN_END_OF_INPUT;
		}
		
		if (($key = array_search($this->tokens[$offset], $this->keywords))) {
			return $key;
		}
		
		if ($this->tokens[$offset]{0} == '$') {
			return self::TOKEN_VARIABLE;
		}
		return self::TOKEN_IDENTIFIER;
	}
	
	/**
	 * Returns the current token as string
	 *
	 * @return string an identifier or a string token
	 */
	public function identifier() {
		return $this->tokens[0];
	}
		 
	/**
	 * Removes the next n token(s) from stream
	 * 
	 * @param $amount number of token that have to be removed
	 *
	 */
	public function next($amount=1) {
		$this->tokens = array_slice($this->tokens, $amount);		
	}
}
?>